<?php
    require_once '../frm/init.php';
    require_once 'clases/Perfil.php';
    include ('includes/inc_session.php');

    $params = $ioHelper->get('params');
    $urlListado = "usuarios.php" . ($params ? "?" . $params : "");

    $id         = $ioHelper->get('id',0);
    $guid       = $ioHelper->get('encrypt',0);
    $reg        = new Ftl_UsuarioBO($id,$guid);

    
    if ($ioHelper->get('action') == 'edit')
    {
        
        $reg->setNombre     ( $ioHelper->getEscaped('txtNombre') );
        $reg->setApellido   ( $ioHelper->getEscaped('txtApellido') );
        $reg->setUsuario    ( $ioHelper->getEscaped('txtUsuario') );
        $reg->setClave      ( $ioHelper->getEscaped('txtClave') );
        $reg->setIdPerfil   ( $ioHelper->getEscaped('cmbPerfil') );
        $reg->setEmail      ( $ioHelper->getEscaped('txtEmail') );

        if ($reg->getId() == 0)
            $reg->setFechaAlta  ( date('Y-m-d H-i-s') );

        $reg->setFechaUltModificacion( date('Y-m-d H-i-s') );

        $respuesta = $reg->guardar();

        echo Ftl_JsonUtil::encode($respuesta);

        exit();
    }
    

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Internet Dreams</title>
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen" title="default" />
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.13.custom.css" rel="stylesheet" />
<!--[if IE]>
<link rel="stylesheet" media="all" type="text/css" href="css/pro_dropline_ie.css" />
<![endif]-->

<!--  jquery core -->
<script src="../js/jquery.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="../js/JS.js"></script>
<!--  checkbox styling script -->
<script src="js/jquery/jquery.bind.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
JS.cargarModulos('../js','UI,Form');


$(document).ready(function(){
        $('#formulario').ajaxForm({
                url         : 'usuarios-am.php?action=edit&id=<?php echo $id;?>',
                type        : 'POST',
                dataType    : 'json',
		beforeSubmit: validate,
		success     : hecho
	});
});

function hecho(data){
   	UI.removeLoader('#button');

	if (data){
            switch(data.state){
                case 1:
                    JS.redirect('<?php echo $urlListado;?>');
                    break;
                case -1:
                    UI.alert('Ocurrieron errores durante el proceso.',{title:'Error'});
                    break;
                default:
                    UI.alert(data.message,{title:'Atención'});
                    break;

            }

        }
}



function validate(formData, jqForm, options) {
    var error = "";

    UI.setLoader('button', {sImgPath:'css/ajax-loader.gif'});

    //var form = jqForm[0];


        var campos = {
                fields: [
                    {
                        nombre:"txtNombre",
                        tipo: "",
                        mensajeVacio: "El nombre no puede estar vacío."
                    },
                    {
                        nombre:"txtApellido",
                        tipo: "",
                        mensajeVacio: "El apellido no puede estar vacío."
                    },
                    {
                        nombre:"txtUsuario",
                        tipo: "",
                        mensajeVacio: "El usuario no puede estar vacío."
                    },
                    {
                        nombre:"txtEmail",
                        tipo: "email",
                        mensajeErroneo: "El formato del email es incorrecto."
                    },
                    {
                        nombre:"cmbPerfil",
                        tipo: "",
                        mensajeVacio: "Debes seleccionar el perfil."
                    }

                ]

        };

        
        if ( $("#txtClave").length )
        {
            campos.fields.push ({
                        nombre:"txtClave",
                        tipo: "clave",
                        mensajeVacio: "Debes ingresar una nueva clave.",
                        mensajeErroneo: "La clave debe tener al menos 6 lestras."
                        
            });
        }

   
    if(!$.fn.validar(campos))
    {
        UI.alert(campos.message,'<p>@</p>',{title:'Atención'});
    	UI.removeLoader('button');
		
        return false;
    }
    else
        return true;

}
</script>
<!--  styled select box script version 2 -->
<script src="js/jquery/jquery.selectbox-0.5_style_2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('.styledselect_form_1').selectbox({ inputClass: "styledselect_form_1" });
});
</script>



<!-- Custom jquery scripts -->
<script src="js/jquery/custom_jquery.js" type="text/javascript"></script>


<!-- MUST BE THE LAST SCRIPT IN <HEAD></HEAD></HEAD> png fix -->
<script src="js/jquery/jquery.pngFix.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document).pngFix( );
});
</script>
</head>
<body>
<?php include ('includes/inc_nav.php');?>

<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div id="content">


<div id="page-heading"><h1>Alta/Modifación</h1></div>


<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
<tr>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
	<th class="topleft"></th>
	<td id="tbl-border-top">&nbsp;</td>
	<th class="topright"></th>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
</tr>
<tr>
	<td id="tbl-border-left"></td>
	<td>
	<!--  start content-table-inner -->
	<div id="content-table-inner">

	<table border="0" width="100%" cellpadding="0" cellspacing="0">
	<tr valign="top">
	<td>


		<!--  start step-holder -->
		<div id="step-holder" style="display:none;">
			<div class="step-no">1</div>
			<div class="step-dark-left"><a href="">Add product details</a></div>
			<div class="step-dark-right">&nbsp;</div>
			<div class="step-no-off">2</div>
			<div class="step-light-left">Select related products</div>
			<div class="step-light-right">&nbsp;</div>
			<div class="step-no-off">3</div>
			<div class="step-light-left">Preview</div>
			<div class="step-light-round">&nbsp;</div>
			<div class="clear"></div>
		</div>
		<!--  end step-holder -->

		<!-- start id-form -->
                <form id="formulario" name="formulario" method="POST" enctype="multipart/form-data">
		<table border="0" cellpadding="0" cellspacing="0"  id="id-form">
		<tr>
			<th valign="top">Nombre:</th>
			<td><input id="txtNombre" name="txtNombre" type="text" class="inp-form" value="<?php echo $ioHelper->output( $reg->getNombre() );?>" /></td>
		</tr>
		<tr>
			<th valign="top">Apellido:</th>
			<td>
                            <input id="txtApellido" name="txtApellido" type="text" class="inp-form" value="<?php echo $ioHelper->output( $reg->getApellido() );?>"/>
                        </td>
		</tr>
		<tr>
			<th valign="top">Usuario:</th>
			<td>
                            <input id="txtUsuario" name="txtUsuario" type="text" class="inp-form" value="<?php echo $ioHelper->output( $reg->getUsuario() );?>"/>
                        </td>
		</tr>
		<?php
                    
                    if ($reg->getId() == 0) { ?>
                        <tr>
                                <th valign="top">Clave:</th>
                                <td>
                                    <input id="txtClave" name="txtClave" type="text" class="inp-form" value="<?php echo $ioHelper->output( $reg->getClave() );?>"/>
                                </td>
                        </tr>
                <?php } ?>
		<tr>
			<th valign="top">Email:</th>
			<td>
                            <input id="txtEmail" name="txtEmail" type="text" class="inp-form" value="<?php echo $ioHelper->output( $reg->getEmail() );?>"/>
                        </td>
		</tr>
		<tr>
		<th valign="top">Perfil:</th>
		<td>
		<select id="cmbPerfil" name="cmbPerfil" class="styledselect_form_1">
			<option value="">--</option>
			<?php
                            
                            foreach(Perfil::getLista() as $k => $v)
                            {
                                echo "<option value=\"$k\" " . ( $reg->getIdPerfil()  == $k ? "selected" : "") . ">$v</option>";
                            }
                        ?>
		</select>
		</td>
		</tr>

	<tr>
		<th>&nbsp;</th>
		<td valign="top">
			<input id="btnSubmir" type="submit"  value="" class="form-submit" />
                        <input id="btnReset" type="reset" value="" class="form-reset"  onclick="document.location = '<?php echo $urlListado;?>';" />
		</td>
	</tr>
	</table>
                </form>
	<!-- end id-form  -->

	</td>
	<td>



</td>
</tr>
<tr>
<td><img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
<td></td>
</tr>
</table>

<div class="clear"></div>


</div>
<!--  end content-table-inner  -->
</td>
<td id="tbl-border-right"></td>
</tr>
<tr>
	<th class="sized bottomleft"></th>
	<td id="tbl-border-bottom">&nbsp;</td>
	<th class="sized bottomright"></th>
</tr>
</table>















<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer -->


</body>
</html>